package cn.sunlight.mm.service.impl;

import cn.sunlight.framework.annotation.HmComponent;
import cn.sunlight.mm.base.BaseService;
import cn.sunlight.mm.common.QuestionConst;
import cn.sunlight.mm.dao.QuestionDao;
import cn.sunlight.mm.dao.ReviewLogDao;
import cn.sunlight.mm.database.MmDaoException;
import cn.sunlight.mm.pojo.ReviewLog;
import cn.sunlight.mm.service.ReviewLogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.Map;

/**
 * @author sunlight
 * @description:
 * @date 2021/5/26
 * @Version v1.0
 */
@Slf4j
@HmComponent("reviewLogService")
public class ReviewLogServiceImpl extends BaseService implements ReviewLogService {

    @Override
    public void addReviewLog(ReviewLog reviewLog) {
        SqlSession session = getSession();
        try {
            ReviewLogDao reviewLogDao = getDao(session, ReviewLogDao.class);
            reviewLogDao.insertReviewLog(reviewLog);
            // 更新主表审核状态
            QuestionDao questionDao = getDao(session, QuestionDao.class);
            Map map=new HashMap();
            map.put("questionId",reviewLog.getQuestionId());
            if (reviewLog.getStatus()== QuestionConst.ReviewStatus.REVIEWED.ordinal()){
                map.put("status",QuestionConst.Status.PUBLISHED.ordinal());
            } else if (reviewLog.getStatus()== QuestionConst.ReviewStatus.REJECT_REVIEW.ordinal()){
                map.put("status",QuestionConst.Status.PRE_PUBLISH.ordinal());
            }
            map.put("reviewStatus",reviewLog.getStatus());
            questionDao.updateStatusAndReviewStatus(map);
            commitAndCloseSession(session);
        }catch (MmDaoException e){
            rollbackAndCloseSession(session);
            e.printStackTrace();
            log.error(e.getMessage());
            throw new MmDaoException(e.getMessage());
        }
    }

}
